#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_checked_data(date):
    print "get_checked_data begin : %s"%date
    
    stat_conn = set_stats_conn()
    shop_conn = set_shop_conn()
    stat_table = "shop_check_log"
    
    #0,uncheck 1,pass 2,reject
    query = "select logid,userid,businessType,logType,entityid,date(addTime) from log where businessType in (4,41,42) and date(addtime)='%s'"%date
    items = shop_conn.get_results(query)
    
    for item in items:
        log_id = item[0]
        user_id = item[1]
        bus_type = item[2]
        log_type = item[3]
        entity_id = item[4]
        add_date = item[5]
        
        param = [log_id,user_id,bus_type,log_type,entity_id,add_date]
        stat_conn.insert_record(stat_table,param)
    
    shop_conn.close()
    stat_conn.close()
    print "get_checked_data end : %s"%date

#shop_check_stat
def get_shop_check_stat(date):
    print "get_checked_data begin : %s"%date
    
    stat_conn = set_stats_conn()
    shop_conn = set_shop_conn()
    stat_table = "shop_check_stat"
    shop_type = 4
    person_type = 41
    merchant_type = 42
    
    update_op = 2
    
    pass_op = 5
    reject_op = 6
    
    query = "select count(1) from shop where userid>0 and date(addtime)='%s'"%date
    items = shop_conn.get_results(query)
    shop_add_count = items[0][0]
    
    person_add_count = 0
    merchant_add_count = 0
    query = "select identifyType,count(1) from identification where date(addTime)='%s' group by identifyType;"%date
    #print query
    items = shop_conn.get_results(query)
    if len(items) > 0:
        person_add_count = items[0][1]
        merchant_add_count = items[1][1]
    
    #print merchant_add_count
    
    shop_update_count = 0
    shop_pass_count = 0
    shop_reject_count = 0
    shop_open_count = 0
    person_update_count = 0
    person_pass_count = 0
    person_reject_count = 0 
    merchant_update_count = 0
    merchant_pass_count = 0
    merchant_reject_count = 0 
    
    query ="SELECT business_type,log_type,count(1) FROM `shop_check_log` where add_date='%s' group by business_type,log_type "%date
    items = stat_conn.get_results(query)
    for item in items:
        #user_id = item[0]
        business_type = item[0]
        log_type = item[1]
        cnt  = item[2]
        
        if business_type == shop_type:      #shop info
            if log_type == update_op:
                shop_update_count += cnt
            elif log_type == 7:
                shop_pass_count += cnt
            elif log_type == 6:
                shop_reject_count += cnt
            elif log_type == 5:
                shop_open_count += cnt
                
        elif business_type == person_type:      #person info
            if log_type == update_op:
                person_update_count += cnt
            elif log_type == pass_op:
                person_pass_count += cnt
            elif log_type == reject_op:
                person_reject_count += cnt
        elif business_type == merchant_type:      #merchant info
            if log_type == update_op:
                merchant_update_count += cnt
            elif log_type == pass_op:
                merchant_pass_count += cnt
            elif log_type == reject_op:
                merchant_reject_count += cnt
    
    print shop_open_count
    #open stat_type=0
    open_check_count = shop_pass_count + shop_reject_count #+ shop_open_count
    open_submit_count= shop_add_count #+ shop_update_count
    open_uncheck_count = shop_add_count - shop_pass_count
    if open_uncheck_count < 0:
        open_uncheck_count =0
        
    open_pass_rate = 0.0
    if open_check_count>0:
        open_pass_rate = shop_open_count/(open_check_count+0.0)
    open_done_rate = 0
    if open_submit_count>0:
        open_done_rate = open_check_count/(open_submit_count+0.0)
    param = [date,open_check_count,open_submit_count,open_uncheck_count,shop_open_count,shop_reject_count,open_pass_rate,open_done_rate,0]
    stat_conn.insert_record(stat_table,param)
    #shop info stat_type=1
    shop_pass_rate = 0.0
    if open_check_count>0:
        shop_pass_rate = shop_pass_count/(open_check_count+0.0)
    param = [date,open_check_count,open_submit_count,open_uncheck_count,shop_pass_count,shop_reject_count,shop_pass_rate,open_done_rate,1]
    stat_conn.insert_record(stat_table,param)
    #person info stat_type=2
    person_check_count =person_pass_count +person_reject_count
    person_submit_count = person_add_count #+ person_update_count
    #print person_update_count,person_submit_count
    person_uncheck_count = person_add_count - person_pass_count
    if person_uncheck_count < 0:
        person_uncheck_count = 0;
    person_pass_rate = 0.0
    if person_check_count > 0:
        person_pass_rate = person_pass_count/(person_check_count+0.0)
        #print person_pass_rate
    person_done_rate = 0.0
    if person_submit_count >0:
        person_done_rate = person_check_count/(person_submit_count+0.0)
        #print person_done_rate
    param = [date,person_check_count,person_submit_count,person_uncheck_count,person_pass_count,person_reject_count,person_pass_rate,person_done_rate,2]
    stat_conn.insert_record(stat_table,param)
     
    #merchant info stat_type=3
    merchant_check_count =merchant_pass_count +merchant_reject_count
    merchant_submit_count = merchant_add_count #+ merchant_update_count
    merchant_uncheck_count = merchant_add_count - merchant_pass_count
    if merchant_uncheck_count< 0:
        merchant_uncheck_count =0
        
    merchant_pass_rate = 0.0
    if merchant_check_count > 0:
        merchant_pass_rate = merchant_pass_count/(merchant_check_count+0.0)
    merchant_done_rate = 0.0
    if merchant_submit_count >0:
        merchant_done_rate = merchant_check_count/(merchant_submit_count+0.0)
    param = [date,merchant_check_count,merchant_submit_count,merchant_uncheck_count,merchant_pass_count,merchant_reject_count,merchant_pass_rate,merchant_done_rate,3]
    stat_conn.insert_record(stat_table,param)
    
    shop_conn.close()
    stat_conn.close()
    print "get_checked_data end : %s"%date

#shop_check_stat
def get_shop_check_work_stat(date):
    print "get_shop_check_work_stat begin : %s"%date
    
    stat_conn = set_stats_conn()
    shop_conn = set_shop_conn()
    stat_table = "shop_check_work_stat"
    shop_type = 4
    person_type = 41
    merchant_type = 42        
    update_op = 2
    pass_op = 5
    reject_op = 6
    

    
    query ="SELECT user_id FROM `shop_check_log` where user_id<10000 and log_type in (7,6) and add_date='%s' group by user_id "%date
    items = stat_conn.get_results(query)
    for item in items:
        user_id = item[0]
        query_in = "select realname from admin where adminid=%s"%user_id
        items_in = shop_conn.get_results(query_in)
        user_name = items_in[0][0]
        
        shop_update_count = 0
        shop_pass_count = 0
        shop_reject_count = 0
        shop_open_count = 0
        person_update_count = 0
        person_pass_count = 0
        person_reject_count = 0 
        merchant_update_count = 0
        merchant_pass_count = 0
        merchant_reject_count = 0 
        query_in ="SELECT business_type,log_type,count(1) FROM `shop_check_log` where user_id=%s and add_date='%s' group by business_type,log_type "%(user_id,date)
        items_in = stat_conn.get_results(query_in)
       
        for item_in in items_in:
            business_type = item_in[0]
            log_type = item_in[1]
            cnt  = item_in[2]           
                
            if business_type == shop_type:      #shop info
                if log_type == update_op:
                    shop_update_count += cnt
                elif log_type == 7:
                    shop_pass_count += cnt
                elif log_type == 6:
                    shop_reject_count += cnt
                elif log_type == 5:
                    shop_open_count += cnt
                    
            elif business_type == person_type:      #person info
                if log_type == update_op:
                    person_update_count += cnt
                elif log_type == pass_op:
                    person_pass_count += cnt
                elif log_type == reject_op:
                    person_reject_count += cnt
            elif business_type == merchant_type:      #merchant info
                if log_type == update_op:
                    merchant_update_count += cnt
                elif log_type == pass_op:
                    merchant_pass_count += cnt
                elif log_type == reject_op:
                    merchant_reject_count += cnt
        
        query = "select count(1) from shop_check_log  where business_type=4 and add_date='%s' and user_id=%s and log_type in (7,6)"%(date,user_id)
        items = stat_conn.get_results(query)
        shop_check_count = items[0][0]
        
        
        #shop_check_count = shop_pass_count + shop_reject_count
        param=[date,user_id,user_name,shop_open_count,shop_check_count,shop_pass_count,shop_reject_count,person_pass_count,person_reject_count,merchant_pass_count,merchant_reject_count]
        stat_conn.insert_record(stat_table,param)
    #open stat_type=0

    
    shop_conn.close()
    stat_conn.close()
    print "get_shop_check_work_stat end : %s"%date

#logic
def execute(date):
    #get_shop_check_work_stat(date)
    try:
        pass
        get_checked_data(date)
    except Exception,e:
        print 'get_checked_data:',e
    try:
        pass
        get_shop_check_stat(date)
    except Exception,e:
        print 'get_shop_check_stat:',e
    try:
        pass
        get_shop_check_work_stat(date)
    except Exception,e:
        print 'get_shop_check_work_stat:',e

def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()

